import {defineStore} from "pinia";

interface userInfo {
    cellPhone: string
    imageUrl: string
    nickName: string
    userCode: string
}

export const userStore = defineStore('nuxtStore', {
    state: (): { isLogin: boolean; token: any; visiable: boolean; visiableNotClose: boolean,userInfo:any } => ({
        token: {},
        isLogin: false,
        visiable: false,
        visiableNotClose: false,
        userInfo:{}
    }),
    actions: {
        setToken(user: userInfo) {
            this.token.cellPhone = user.cellPhone
            this.token.imageUrl = user.imageUrl
            this.token.nickName = user.nickName
            this.token.userCode = user.userCode
        },
        setIsLogin(isLogin: boolean) {
            this.isLogin = isLogin
        },
        getIsLogin() {
            if (this.isLogin == false || JSON.stringify(this.token) === '{}') {
                console.log("用户没有登录")
                this.visiable = true
                return false;  // 直接返回 false，表示未登录
            }
            return true;  // 返回 true 表示已登录
        },
        setUserInfo(userInfo:any){
            this.userInfo = userInfo
        }
    },
    // 注意：persist定义要做判断，因为localStorage/sessionStorage是客户端参数，所以需要加process.client
    persist: process.client && {
        storage: sessionStorage,
    },
})



